/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sp.validasi;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import sp.component.TextFieldListing;
import sp.dao.KortimDao;
import sp.form.Hal1Listing;
import sp.model.Dsartb3;

/**
 *
 * @author Ristika Nugraha [12.7350]
 */
public class ValListB3 {
    Dsartb3 b3;
    Hal1Listing b3view;
    TextFieldListing dummy;
    private ArrayList<sp.model.Error> b1listerr;

    public ValListB3(Dsartb3 b3,Hal1Listing b3view) {
        this.b3 = b3;
        this.b3view = b3view;
    }
    
    public ArrayList<sp.model.Error> cek() {
        b1listerr = new ArrayList<>();

        cekb3r2();

        return b1listerr;
    }
    
    private TextFieldListing getError(TextFieldListing tx) {
        tx.setBackground(Color.red);
        return tx;
    }
    
    private void cekb3r2() {
        String nimKortim = dotKortim(b3.getB3r2());
        String sql = "SELECT k FROM Kortim k WHERE k.nimkortim =\'" + nimKortim + "\'";
        List<Object> kodeKortim = (List<Object>) KortimDao.getInstance().entity().createQuery(sql).getResultList();
        if(kodeKortim.isEmpty()){
            dummy = getError(b3view.getB3r2());
            b3view.setB3r2(dummy);
            b1listerr.add(b1listerr.size(), new sp.model.Error("b3r2", "Kesalahan NIM Kortim\nSIlahkan Cek Kembali NIM Kortim", 1));
        }
    }
    
    private String dotKortim(String nim) {
        String awal = nim.substring(0, 2);
        String akhir = nim.substring(2);
        return awal + '.' + akhir;
    }
}
